package com.eric.server.UI;


import com.eric.server.logic.DefaultLogic;
import com.eric.server.logic.ILogic;
import com.eric.server.monitor.MonitorListener;
import com.eric.server.monitor.MonitorServer;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by eric on 2017/11/15.
 */
public class MonitorUI extends JFrame implements MonitorListener {

    JTextField fieldPort ;
    JTextField fieldRoom ;
    JTextArea areaRecord ;
    JTextArea areaRoom ;
    JButton start,stop;

    MonitorServer server ;
    boolean isStart = false ;

    DefaultLogic logic = DefaultLogic.me();

    public MonitorUI(String title){
        super(title);
        initUI();

        logic.setListener(this);
    }

    public MonitorUI(){
        super("监视器");
    }

    public void setServer(MonitorServer server){
        this.server = server;
    }

    private void initUI(){
        initHead();
        initBody();
        changeBtnStatus();
    }

    private void initHead(){
        Box horizontalBox =  Box.createHorizontalBox();
        horizontalBox.add(Box.createHorizontalStrut(5));

        fieldPort = new JTextField();
        horizontalBox.add(new JLabel("端口:"));
        horizontalBox.add(fieldPort);
        start = new JButton("启动");
        stop = new JButton("停止");

        start.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String portStr = fieldPort.getText();
                if(portStr == null || "".equals(portStr)){
                    JOptionPane.showMessageDialog(MonitorUI.this,"耍我?  端口都没填,我监控啥呢...");
                    return ;
                }
                int port = Integer.parseInt(portStr);
                if(server != null){
                    try{
                        server.startServer(port);
                    }catch (Exception ee){
                        ee.printStackTrace();
                    }
                }
                isStart = true;
                changeBtnStatus();
                notice("准备启动...");
            }
        });
        horizontalBox.add(start);

        stop.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(server != null){
                    server.stopServer();
                }
                isStart = false;
                changeBtnStatus();
                notice("准备停止...");
            }
        });
        horizontalBox.add(stop);

        horizontalBox.add(Box.createHorizontalStrut(10));

        fieldRoom = new JTextField();
        JButton create = new JButton("创建房间");
        create.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String roomName = fieldRoom.getText();
                if(roomName == null || "".equals(roomName)){
                    JOptionPane.showMessageDialog(MonitorUI.this,"你傻呀,  房间号还没填呢...");
                    return ;
                }
                logic.createRoom(roomName);
                createRoom(roomName);
            }
        });
        horizontalBox.add(new JLabel("房间号"));
        horizontalBox.add(fieldRoom);
        horizontalBox.add(create);
        horizontalBox.add(Box.createHorizontalStrut(5));


        this.getContentPane().add(horizontalBox, BorderLayout.NORTH);
    }

    private void initBody(){
        areaRecord = new JTextArea(10,10);
        areaRecord.setEditable(false);
        this.getContentPane().add(new JScrollPane(areaRecord),BorderLayout.CENTER);

        areaRoom = new JTextArea(10,10);
        areaRoom.setBackground(Color.GRAY);
        areaRecord.setEditable(false);
        this.getContentPane().add(new JScrollPane(areaRoom),BorderLayout.EAST);
    }

    private void changeBtnStatus(){
        if(isStart){
            stop.setEnabled(true);
            start.setEnabled(false);
        }else{
            stop.setEnabled(false);
            start.setEnabled(true);
        }
    }

    public void showMonitor(){
        this.setSize(500,350);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void notice(String msg){
        areaRecord.append(msg);
        areaRecord.append("\n");
        areaRecord.setCaretPosition(areaRecord.getDocument().getLength());
    }

    public void started(String ip,int port) {
        notice("服务启动完成. host : "+ip+"  port:"+port);
        notice("-------------------------------------------");
    }

    public void stoped() {
        notice("-------------------------------------------");
        notice("服务已经停止.");
    }

    private void createRoom(String room){
        areaRoom.append("创建房间 : "+room);
        areaRoom.append("\n");
    }

    public static void main(String []s){
        MonitorUI monitor = new MonitorUI("监视服务");
        monitor.showMonitor();
    }
}
